# -*- coding: utf-8 -*-
import scrapy
from ..items import Jingdong2Item


class MeishiSpider(scrapy.Spider):
    name = 'meishi'
    start_urls = [
        'https://search.jd.com/Search?keyword=%E7%BE%8E%E9%A3%9F&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E7%BE%8E%E9%A3%9F&stock=1&page=1&s=1&click=0',
        # 'https://search.jd.com/Search?keyword=%E7%BE%8E%E9%A3%9F&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E7%BE%8E%E9%A3%9F&stock=1&page=3&s=56&click=0',
        # 'https://search.jd.com/Search?keyword=%E7%BE%8E%E9%A3%9F&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E7%BE%8E%E9%A3%9F&stock=1&page=5&s=112&click=0',
        # 'https://search.jd.com/Search?keyword=%E7%BE%8E%E9%A3%9F&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E7%BE%8E%E9%A3%9F&stock=1&page=7&s=164&click=0',
        # 'https://search.jd.com/Search?keyword=%E7%BE%8E%E9%A3%9F&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E7%BE%8E%E9%A3%9F&stock=1&page=9&s=217&click=0'
    ]

    def parse(self, response):
        all_li = response.xpath("//ul[@class='gl-warp clearfix']/li[@class='gl-item hover']")
        for li in all_li:
            title = li.xpath(".//div[@class='p-name p-name-type-2']/text()")[0].extract()
            price = li.xpath(".//div[@class='p-price']/strong/i/text()")[0].extract()
            dianpu = li.xpath(".//div[@class='p-shop']/span[@class='J_im_icon']/text()")[0].extract()
            pinglun = li.xpath(".//div[@class='p-commit']/strong/a[@id='J_comment_100000244262']/text()")[0].extract()
            print(title, price, dianpu, pinglun)

            item = Jingdong2Item()
            item['title'] = title
            item['price'] = price
            item['dianpu'] = dianpu
            item['pinglun'] = pinglun
            yield item
